<template>
	<view class="sbu-home-content">
		<view class="home-top">
			<view class="home-title">超级返</view>
			<image src="../../static/common/sub-back-icon.png" class="sub-back-icon" @tap="goHome"></image>
			<image src="../../static/common/sbu-home-top-icon.png" class="sub-home-top-icon" @tap="contactKF"></image>
		</view>
		<view class="posit-box">
			<image src="../../static/common/sbu-home-banner.png" class="sub-home-banner" @tap="makeMoney"></image>
			<text class="weekCount">{{weekCount}}</text>
		</view>
		
		<view class="home-notes" @click="notesDetail">
			<image src="../../static/common/sbu-notes-icon.png" class="sub-notes-icon"></image>
			<view class="notes">{{title}}</view>
		</view>
		
		<view class="nav-tab">
			<view class="buying" :class="{'select':buyIng}" @tap="dobuy">抢购中</view>
			<view class="buying" @tap="dobuy" :class="{'select':willBuying}">即将开枪</view>
		</view>
		<!-- 抢购中 -->
		<scroll-view class="goods-block buy-ing" 
		scroll-y="true"
		@scrolltolower="getNewList(1)"
		refresher-enabled="true" :refresher-triggered="triggered"
		:refresher-threshold="100" 
		@refresherrefresh="onRefresh" @refresherrestore="onRestore" 
		v-if="buyIng">
			<view class="buying-box" v-if="orderList_on.length>0">
				<view class="goods-box" @tap="goodDetail(goods)" v-for="(goods,index) in orderList_on" :key="index">
					<view class="good-info">
						<image :src="getImg(goods.giftImg)" class="sub-good-img"></image>
						<view class="good-quion">返￥{{goods.productActualPrice&&goods.price?calculate(goods.productActualPrice,goods.price):'-'}}</view>
						<view class="info-right">
							<view class="good-name">
								<image src="../../static/common/sbu-tao.png" class="tao-icon"></image>
								<view class="g-name">{{goods.giftName?goods.giftName:'-'}}</view>
							</view>
							<view class="good-price">
								<view class="good-left">
									<view class="old-price">到手价 ￥{{goods.price?goods.price:'-'}}</view>
									<view class="new-price">实拍价 ￥{{goods.productActualPrice?goods.productActualPrice:'-'}}</view>
								</view>
								<view class="good-right">
									<view class="scale-states">剩余{{goods.unfinishCount}}/{{goods.orderCount}}</view>
									<image src="../../static/common/buying-img.png" class="buying-img"></image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="nodata" v-else>
				<image src="../../static/common/nodata-sub.png" class="nodata-img"></image>
			</view>
		</scroll-view>
		<!-- 即将开枪 -->
		<scroll-view class="goods-block will-buying" 
		scroll-y="true"
		refresher-enabled="true" :refresher-triggered="triggered"
		:refresher-threshold="100" 
		@refresherrefresh="onRefresh" @refresherrestore="onRestore" 
		@scrolltolower="getNewList(2)"
		v-if="willBuying">
			
			<view class="buying-box"  v-if="orderList_no.length>0">
				<view class="goods-box goods-box2" 
				v-for="(goods,index) in orderList_no" :key="index"
				@tap="goods.buy_ready?goodDetail(goods):''"
				>
					<view class="good-info">
						<image :src="getImg(goods.giftImg)" class="sub-good-img"></image>
						<view class="good-quion">返￥{{goods.productActualPrice&&goods.price?calculate(goods.productActualPrice,goods.price):'-'}}</view>
						<view class="info-right">
							<view class="good-name">
								<image src="../../static/common/sbu-tao.png" class="tao-icon"></image>
								<view class="g-name">{{goods.giftName?goods.giftName:'-'}}</view>
							</view>
							<view class="good-price" style="margin-top: 20upx;">
								<view class="good-left">
									<view class="old-price">到手价 ￥{{goods.price?goods.price:'-'}}</view>
									<view class="new-price">实拍价 ￥{{goods.productActualPrice?goods.productActualPrice:'-'}}</view>
								</view>
								<view class="good-right">
									<view src="../../static/common/will-buying-btn.png" 
									:class="{'buying-ready':goods.buy_ready}"
									@tap="goods.buy_ready?goodDetail(goods):setAttion(goods)"
									class="willimg">{{text}}</view>
									<!-- <view class="scale-states buy-titme">{{str}}{{handleTime(goods.releaseTime,index)}}</view>getCountDown -->
									
								</view>
								
							</view>
							<view class="scale-states buy-titme">{{goods.str}}{{getCountDown(goods.releaseTime,index)}}</view>
						</view>
					</view>
				</view>
			</view>
			<view class="nodata" v-else>
				<image src="../../static/common/nodata-sub.png" class="nodata-img"></image>
			</view>
		</scroll-view>
		
		<giftList class="gift-block"
			v-if="areaIshow && isGiftList"
			@closeArea="closeArea"
			@qureryList="qureryList"
		></giftList>
		
		<drag-button
			:isDock="true"
			:existTabBar="true"
			@btnClick="btnClick"
		/>
	</view>
</template>

<script>
	import giftList from'../gift-list/gift-list.vue'
	import dragButton from "../drag-button/drag-button.vue";
	export default{
		name:"subHome",
		data(){
			return{
				title:"",//公告标题
				articleId:"",
				buyIng:true,
				willBuying:false,
				orderList_on:"",//抢购中
				orderList_no:"",//即将开枪
				pageIndex:1,
				type_tag:"",//分页加载标识
				apptoken:"",
				day:"",
				hours:"",
				minute:"",
				secound:"",
				str:"",
				text:"即将开抢",
				buy_ready:false,
				nodata:false,
				areaIshow:true,
				giftList:"",
				list1:"",
				list2:"",
				listType:0,
				mainInfo:"",
				isGiftList:"",
				triggered: true,
				freshing:false,
				unFinishedStatus:"",
			}
		},
		components:{giftList,dragButton},
		mounted() {
			
			this.apptoken = uni.getStorageSync("apptoken");
			this.type_tag = "orderList_on";
			//初始化加载抢购中的商品列表
			this.getOrdering();
			this.getMessage();
			this.getGiftList();
			//查看用户是否有未操作订单
			this.checkOrderStatus();
		},
		methods:{
			getStr(index){
				
			},
			checkOrderStatus(type){
				const _this = this;
				uni.request({
					url:this.$API.URL + this.$API.user_orderCount,
					data:{apptoken:this.apptoken},
					success(res) {
						console.log(res);
						if(res.data.success){
							//将数量存入navBar数组，渲染
							const d = res.data.data;
							if(d.dczOrderCount>0){
								uni.showModal({
									title:"温馨提示",
									content:"你还有待操作的订单未完成，请先去完成",
									success(res) {
										if(res.cancel){
											_this.$parent.goOrder(2,0);
										}else if(res.confirm){
											_this.$parent.goOrder(2,0);
										}
									}
								})
							}
						
						}
					},
					fail(res) {
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}
				})
			},
			onRefresh() {
					if (this.freshing) return;
					this.freshing = true;
					if(this.listType == 0){
						this.getOrdering(null,null,"onRefresh");
					}else if(this.listType == 1){
						this.getWillOrder(null,null,"onRefresh")
					}
					
			},
			onRestore() {
					this.triggered = 'restore'; // 需要重置
			},
			getGiftList(){
				//获取礼品列表
				const _this = this;
				uni.request({
					url:this.$API.URL + this.$API.giftList,
					success(res) {
						console.log(res)
						if(res.data.data.length<1){
							_this.isGiftList = false;
						}else{
							_this.isGiftList = true;
						}
					},
					fail(res) {
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}
				})
			},
			calculate(v1,v2){
				//return Number(v1) - Number(v2);
				return (Number(v1)*1000 - Number(v2)*1000)/1000;
				
			},
			closeArea(){
				this.areaIshow = false;
			},
			btnClick() {
				const _this = this;		
				this.areaIshow = true;
				uni.request({
					url:this.$API.URL + this.$API.giftList,
					success(res) {
						console.log(res)
						if(res.data.data.length<1){
							_this.isGiftList = false;
							_this.$CommonJS.showToast("暂无礼品")
						}else{
							_this.isGiftList = true;
						}
					},
					fail(res) {
						uni.showToast({
							title:res,
							icon:'none'
						})
					}
				})
				
			},
			getImg(url){
				//处理图片路劲
				return this.$CommonJS.getBaseImgUrl(url);
				
			},
			qureryList(giftId){
				this.pageIndex = 1;
				console.log(giftId)
				console.log("this.listType=====" + this.listType)
				if(this.listType == 0){
					// this.orderList_on = this.list1.filter((item)=>{
					// 	 return item.giftLevel == leavel;
					// })
					this.getOrdering(giftId,null,null);
				}else if(this.listType == 1){
					// this.orderList_no = this.list2.filter((item)=>{
					// 	 return item.giftLevel == leavel;
					// })
					this.getWillOrder(giftId,null,null)
				}
			
				//console.log(this.orderList_on)
			},
			setAttion(){
				//设置提醒
			},
			handleTime(time,index){
				const _this = this;
				//处理时间戳，倒计时
				var a = Date.now();
				//const  resultTime  = 1589091661000;
				//const endTimer = resultTime - a;
				
				var endTimer = time - a;
				console.log(endTimer)
				if(endTimer < 0){
					//任务已结束
					this.task_status = !this.task_status;
					console.log("任务已结束")
				}else{
					 var time = setInterval(function(){
					 var nowTime = new Date();
					 //let endTime = time;
						var t = time - nowTime.getTime();
						console.log(t)
						if(t < 0){
							_this.str = "活动开始";
							_this.text = "马上抢"
							_this.buy_ready = true;
							clearInterval(time)
							//_this.orderList_no.splice(index, 1);
							return;
						}
						//let d=Math.floor(t/1000/60/60/24);
						var hour=Math.floor(t/1000/60/60%24);
						var min=Math.floor(t/1000/60%60);
						var sec=Math.floor(t/1000%60);
						//大于3分钟，设置抢购提醒
						if(hour  == 0 && min < 3){
							_this.text = "马上抢"
						}
						_this.str = "剩余" + hour + "小时" + min + "分钟" + sec + "秒";
					},1000);
				}
			},
			getCountDown(timestamp,index){
				const _this = this;
				var nowTime1 = new Date();
				var endTime1 = new Date(timestamp);
				
				var t1 = endTime1.getTime() - nowTime1.getTime();
				if(t1 < 0){
					_this.orderList_no[index].str = "";
					_this.text = "马上抢"
					_this.orderList_no[index].buy_ready = true;
					return;
				}
				
				 var time = setInterval(function(){
						var nowTime = new Date();
						var endTime = new Date(timestamp);
						
						var t = endTime.getTime() - nowTime.getTime();
						if(t < 0){
							_this.orderList_no[index].str = "";
							clearInterval(time)
							return;
						}
//            var d=Math.floor(t/1000/60/60/24);
						var hour=Math.floor(t/1000/60/60%24);
							 var min=Math.floor(t/1000/60%60);
							 var sec=Math.floor(t/1000%60);

						if (hour < 10) {
								 hour = "0" + hour;
						}
						if (min < 10) {
								 min = "0" + min;
						}
						if (sec < 10) {
								 sec = "0" + sec;
						}
						var countDownTime = hour + ":" + min + ":" + sec;
						//$("#countDown1").html(countDownTime);
						//_this.data[index].str = ""
							_this.orderList_no[index].str ="剩余" + hour + "小时" + min + "分钟" + sec + "秒";
					//	_this.str = index;
				
				},1000);
		},
			getOrdering(giftId,type,refsh){
				if(refsh){
					console.log(111)
					this.pageIndex = 1;
				}
				console.log(this.pageIndex)
				////初始化加载抢购中的商品列表
				const _this = this;
				if(this.nodata && !refsh){
					//最后一页停止请求
					return;
				}
				console.log(JSON.stringify({apptoken:this.apptoken,pageIndex:this.pageIndex,giftId:giftId?giftId:''}))
				uni.request({
					url:this.$API.URL + this.$API.orderList_on,
					data:{apptoken:this.apptoken,pageIndex:this.pageIndex,giftId:giftId?giftId:''},
					success(res) {
						console.log(res)
							//_this.$emit('stopPullDown')
						if(res.data.success){
							if(res.data.data.length < 1 && _this.pageIndex !=1){
								//最后一页
								_this.$CommonJS.showToast("没有更多数据了");
								_this.nodata = !_this.nodata;
								_this.triggered = false;
								_this.freshing = false;
							}else{
								if(!type){
									_this.orderList_on = res.data.data;
									_this.list1 = res.data.data;
									
									
								}else{
									//分页
									res.data.data.forEach(item=>{
										_this.orderList_on.push(item)
									})
									
								}
									_this.pageIndex ++;
									if(refsh && refsh == "onRefresh"){
										_this.triggered = false;
										_this.freshing = false;
										uni.showToast({
											title:"刷新成功"
										})
									}
							}
							
						}else{
							_this.triggered = false;
							_this.freshing = false;
							_this.$CommonJS.showToast(res.data.msg)
							console.log(res.data.msg)
						}
					},
					fail(res) {
						uni.showToast({
							title:res,
							icon:'none'
						})
					}
				})
			},
			getWillOrder(giftId,type,refsh){
				if(refsh){
					this.pageIndex = 1;
				}
				//加载即将开抢商品列表
				const _this = this;
				console.log(this.pageIndex)
				if(this.nodata && !refsh){
					//最后一页停止请求数据
					return;
				}
				// this.listType = 1;
				uni.request({
					url:this.$API.URL + this.$API.orderList_no,
					data:{apptoken:this.apptoken,pageIndex:this.pageIndex,giftId:giftId?giftId:''},
					success(res) {
						console.log(res)
						if(res.data.success){
							//res.data.data = _this.data;
							if(res.data.data.length < 1 && _this.pageIndex !=1){
								//最后一页
								_this.$CommonJS.showToast("没有更多数据了");
								_this.nodata = !_this.nodata;
								_this.triggered = false;
								_this.freshing = false;
							}else{
								//添加倒计时标识
								res.data.data.forEach(item=>{
									item.str = "";
									item.buy_ready = false;
								})
								console.log(res.data.data)
								if(!type){
									_this.orderList_no = res.data.data;
									_this.list2 = res.data.data;
								}else{
									//分页
									res.data.data.forEach(item=>{
										_this.orderList_no.push(item)
									})
								}
								_this.pageIndex ++;
								if(refsh && refsh == "onRefresh"){
									_this.triggered = false;
									_this.freshing = false;
									uni.showToast({
										title:"刷新成功"
									})
								}
							}
							
						}else{
							_this.triggered = false;
							_this.freshing = false;
							console.log(res.data.msg)
							_this.$CommonJS.showToast(res.data.msg)
						}
					},
					fail(res) {
						uni.showToast({
							title:res,
							icon:'none'
						})
					}
				})
			},
			getNewList(n){
				if(this.nodata){
					return;
				}
				//分页加载
				if(n == 1){
					//抢购中商品
					this.getOrdering(null,"loadMore",null);
				}else if(n == 2){
					this.getWillOrder(null,"loadMore",null);
				}
			},
			makeMoney(){
				this.$CommonJS.navigateTo('../../pages/subBack/sub-makeMoney/sub-makeMoney');
			},
			goodDetail(goods){
				//任务详情
				const _this = this;
				uni.setStorage({
					key:"task_detail",
					data:goods,
					success() {
						_this.$CommonJS.navigateTo('../../pages/subBack/sub-gooddetail/sub-gooddetail');
					}
				})
				
				
			},
			dobuy(n){
				//Nav切换
				this.buyIng = !this.buyIng;
				this.willBuying = !this.willBuying;
				
				if(this.buyIng){
					this.type_tag = "orderList_on";
					this.pageIndex = 1;
					this.nodata = false;
					this.listType = 0;
					this.getOrdering();
				}else{
					this.type_tag = "orderList_no";
					this.pageIndex = 1;
					this.nodata = false;
					this.listType = 1;
					this.getWillOrder();
				}
			},
			getMessage(){
				//最新公告
				this.title = uni.getStorageSync("mainInfo").Notice;
				console.log(uni.getStorageSync("mainInfo"))
				this.weekCount = uni.getStorageSync("mainInfo").weekCount;
			},
			notesDetail(){
				//公告列表
				uni.navigateTo({
					url:'../../pages/subBack/sub-message/message'
				})
			},
			goHome(){
				uni.switchTab({
					url:'../../pages/home/home'
				})
			},
			contactKF(){
				this.$CommonJS.navigateTo('../../pages/subBack/sub-kefu/sub-kefu')
			}
		}
	}
</script>

<style scoped>
	@import url("sub-home.css");
	.posit-box{
		width: 100%;
		height: auto;
		position: relative;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}
	.weekCount{
		position: absolute;
		left: 295upx;
		top: 115upx;
		font-size: 14px;
		color:#111739;
		font-weight: bold;
	}
	.gift-block{
		position: fixed;
		top: 0;
		z-index: 99999;
		left: 0;
	}
</style>
